package com.device.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.device.domain.dto.CabinetTypeAddUpdateDto;
import com.device.domain.dto.CabinetTypeDto;
import com.device.domain.entity.CabinetType;
import com.device.domain.vo.CabinetTypeVo;
import com.device.mapper.CabinetTypeMapper;
import com.device.service.CabinetTypeService;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;

import java.util.List;

/**
* @author sheng
* @description 针对表【cabinet_type(柜机类型表)】的数据库操作Service实现
* @createDate 2025-05-10 20:47:35
*/
@Service
public class CabinetTypeServiceImpl extends ServiceImpl<CabinetTypeMapper, CabinetType>
    implements CabinetTypeService {

    @Autowired
    private CabinetTypeMapper cabinetTypeMapper;
    /**
     * 查询柜机类型
     *
     * @param
     * @return
     */
    @Override
    @Cacheable(
            value = "cabinetTypePage",
            key = "#cabinetTypeDto.pageNum + '-' + #cabinetTypeDto.pageSize + '-' + (#cabinetTypeDto.name?:'')",
            unless = "#result == null"
    )
    public PageInfo<CabinetTypeVo> selectListOfCabinetMachineTypes(CabinetTypeDto cabinetTypeDto) {

        Page<CabinetType> page = new Page<>(
                cabinetTypeDto.getPageNum(),
                cabinetTypeDto.getPageSize()
        );

        QueryWrapper<CabinetType> queryWrapper = new QueryWrapper<>();
        if (StringUtils.isNotBlank(cabinetTypeDto.getName())) {
            queryWrapper.like("name", cabinetTypeDto.getName());
        }

        // 使用MyBatis-Plus的分页查询方法
        Page<CabinetType> entityPage = cabinetTypeMapper.selectPage(page, queryWrapper);

        // 转换为VO的分页结果
        PageInfo<CabinetTypeVo> pageInfo = new PageInfo<>();
        pageInfo.setList(BeanUtil.copyToList(entityPage.getRecords(), CabinetTypeVo.class));
        pageInfo.setTotal(entityPage.getTotal());
        pageInfo.setPageNum((int) entityPage.getCurrent());
        pageInfo.setPageSize((int) entityPage.getSize());

        return pageInfo;
    }
    /**
     * 柜机类型全查 - 缓存全部类型
     */
    @Override
    @Cacheable(
            value = "cabinetTypeAll",
            unless = "#result == null || #result.isEmpty()"
    )
    public List<CabinetTypeVo> allCheck() {
        List<CabinetType> list = cabinetTypeMapper.selectList(null);
        return BeanUtil.copyToList(list, CabinetTypeVo.class);
    }

    /**
     * 柜机管理新增 - 清除相关缓存
     */
    @Override
    @CacheEvict(
            value = {
                    "cabinetTypePage",
                    "cabinetTypeAll"
            },
            allEntries = true
    )
    public void refundAllCheck(CabinetTypeAddUpdateDto cabinetTypeAddUpdateDto) {
        cabinetTypeMapper.refundAllCheck(cabinetTypeAddUpdateDto);
    }

    /**
     * 柜机管理修改 - 清除相关缓存
     */
    @Override
    @CacheEvict(
            value = {
                    "cabinetTypePage",
                    "cabinetTypeAll"
            },
            allEntries = true
    )
    public void refundUpdateCheck(CabinetTypeAddUpdateDto cabinetTypeAddUpdateDto) {
        cabinetTypeMapper.refundUpdateCheck(cabinetTypeAddUpdateDto);
    }

    /**
     * 批量删除 - 清除相关缓存
     */
    @Override
    @CacheEvict(
            value = {
                    "cabinetTypePage",
                    "cabinetTypeAll"
            },
            allEntries = true
    )
    public void typeDelete(List<Long> ids) {
        cabinetTypeMapper.typeDelete(ids);
    }

    /**
     * 单个删除 - 清除相关缓存
     */
    @Override
    @CacheEvict(
            value = {
                    "cabinetTypePage",
                    "cabinetTypeAll"
            },
            allEntries = true
    )
    public void deletedBy(Integer id) {
        cabinetTypeMapper.deleteById(id);
    }
}




